package com.ruoyi.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.SysInfoArticle;
import com.ruoyi.info.service.ISysInfoArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资讯文章Controller
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/info/article")
public class SysInfoArticleController extends BaseController
{
    @Autowired
    private ISysInfoArticleService sysInfoArticleService;

    /**
     * 查询资讯文章列表
     */
    @PreAuthorize("@ss.hasPermi('info:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysInfoArticle sysInfoArticle)
    {
        startPage();
        List<SysInfoArticle> list = sysInfoArticleService.selectSysInfoArticleList(sysInfoArticle);
        return getDataTable(list);
    }

    /**
     * 导出资讯文章列表
     */
    @PreAuthorize("@ss.hasPermi('info:article:export')")
    @Log(title = "资讯文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysInfoArticle sysInfoArticle)
    {
        List<SysInfoArticle> list = sysInfoArticleService.selectSysInfoArticleList(sysInfoArticle);
        ExcelUtil<SysInfoArticle> util = new ExcelUtil<SysInfoArticle>(SysInfoArticle.class);
        util.exportExcel(response, list, "资讯文章数据");
    }

    /**
     * 获取资讯文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return success(sysInfoArticleService.selectSysInfoArticleByArticleId(articleId));
    }

    /**
     * 新增资讯文章
     */
    @PreAuthorize("@ss.hasPermi('info:article:add')")
    @Log(title = "资讯文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysInfoArticle sysInfoArticle)
    {
        sysInfoArticle.setCreateBy(getUsername());
        return toAjax(sysInfoArticleService.insertSysInfoArticle(sysInfoArticle));
    }

    /**
     * 修改资讯文章
     */
    @PreAuthorize("@ss.hasPermi('info:article:edit')")
    @Log(title = "资讯文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysInfoArticle sysInfoArticle)
    {
        sysInfoArticle.setUpdateBy(getUsername());
        return toAjax(sysInfoArticleService.updateSysInfoArticle(sysInfoArticle));
    }

    /**
     * 删除资讯文章
     */
    @PreAuthorize("@ss.hasPermi('info:article:remove')")
    @Log(title = "资讯文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds)
    {
        return toAjax(sysInfoArticleService.deleteSysInfoArticleByArticleIds(articleIds));
    }

    /**
     * 发布文章
     */
    @PreAuthorize("@ss.hasPermi('info:article:edit')")
    @Log(title = "发布文章", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{articleId}")
    public AjaxResult publish(@PathVariable Long articleId)
    {
        return toAjax(sysInfoArticleService.publishArticle(articleId));
    }

    /**
     * 下架文章
     */
    @PreAuthorize("@ss.hasPermi('info:article:edit')")
    @Log(title = "下架文章", businessType = BusinessType.UPDATE)
    @PutMapping("/unpublish/{articleId}")
    public AjaxResult unpublish(@PathVariable Long articleId)
    {
        return toAjax(sysInfoArticleService.unpublishArticle(articleId));
    }

    /**
     * 更新浏览次数
     */
    @GetMapping("/view/{articleId}")
    public AjaxResult updateViewCount(@PathVariable Long articleId)
    {
        return toAjax(sysInfoArticleService.updateViewCount(articleId));
    }

    /**
     * 查询推荐文章列表
     */
    @GetMapping("/recommend")
    public AjaxResult getRecommendArticles()
    {
        List<SysInfoArticle> list = sysInfoArticleService.selectRecommendArticleList(10);
        return success(list);
    }

    /**
     * 查询置顶文章列表
     */
    @GetMapping("/top")
    public AjaxResult getTopArticles()
    {
        List<SysInfoArticle> list = sysInfoArticleService.selectTopArticleList();
        return success(list);
    }
}
